package erlangtools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ErlangProject {
	private List<Folder> dirs;
	private List<Module> modules;
	private List<BuildOption> buildOps;
	private Map<String, AnalysisResult> analysis;
	private String compilerPath = "/erl5.5.5/bin/erlc.exe";

	public ErlangProject() {
		dirs = new ArrayList<Folder>();
		modules = new ArrayList<Module>();
		buildOps = new ArrayList<BuildOption>();
		analysis = new HashMap<String, AnalysisResult>();
	}
	
	public void addDir(Folder f) {
		dirs.add(f);
	}
	
	public boolean containsDir(String f) {
		return dirs.contains(f);
	}
	
	public List<Folder> getDirs() {
		return dirs;
	}
	
	public void resetDirs() {
		dirs = new ArrayList<Folder>();
	}
	
	public void resetModules() {
		modules = new ArrayList<Module>();
	}
	
	public List<Module> getModules() {
		return modules;
	}
	
	public void addModule(Module m) {
		modules.add(m);
	}

	public void parseBuildOps(String text) {
		buildOps = new ArrayList<BuildOption>();
		String buildOpsText = text;
		String[] ops = buildOpsText.split("\n");
		for (String op: ops) {
			String[] fromTo = op.split("->");
			String from = fromTo[0].trim();
			String to = fromTo[1].trim();
			if (from.charAt(from.length() - 1) != '/') from += '/';
			if (to.charAt(to.length() - 1) != '/') to += '/';
			buildOps.add(BuildOption.createBuildOption(from, to));
		}
	}
	
	public List<BuildOption> getBuildOps() {
		return buildOps;
	}
	
	public void resetAnalysis() {
		analysis = new HashMap<String, AnalysisResult>();
	}
	
	public void putAnalysis(String path, AnalysisResult ar) {
		analysis.put(path, ar);
	}
	
	public Map<String, AnalysisResult> getAnalysis() {
		return analysis;
	}

	public String getCompilerPath() {
		return compilerPath;
	}

	public void setCompilerPath(String compilerPath) {
		this.compilerPath = compilerPath;
	}
	
	public ManyDirectoriesListSet getSearchSet() {
		List<String> dl = new ArrayList<String>();
		for (Folder f: dirs) {
			dl.add(f.getName());
		}
		return new ManyDirectoriesListSet(dl, "*.[eh]rl", true);
	}
	
}
